#include "CSmath.h"

CSmath::CSmath()
{
}

CSmath::~CSmath()
{
}

int CSmath::Min_Alg(int a, int b)
{
	if(a<b)
        return a;
    else
        return b;
}

int CSmath::Max_Alg(int a, int b)
{
	if (a > b)
		return a;
	else
		return b;
}

int CSmath::Mean_Alg(int* member, int qidian, int weidian)
{
	if (qidian != weidian)
	{
		long answer = 0;
		int i;
		for (i = qidian; i < weidian; i++)
			answer = answer + member[i];
		return (int)(answer / (weidian - qidian));
	}
	else
	{
		return member[qidian];
	}
}

int CSmath::Mean_abs_Alg(int* member, int qidian, int weidian)
{
	if (qidian != weidian)
	{
		long answer = 0;
		int i;
		for (i = qidian; i < weidian; i++)
			answer = answer + abs(member[i]);
		return (int)(answer / (weidian - qidian));
	}
	else
	{
		return abs(member[qidian]);
	}
}

int CSmath::GetIndexMin_Alg(int* ppData_x, int index_start, int index_end)
{
	int i = 0;
	int index_result = index_start;
	for (i = index_start; i < index_end; i++)
	{
		if (ppData_x[i] < ppData_x[index_result])
			index_result = i;
	}
	return index_result;
}

int CSmath::GetIndexMax_Alg(int* ppData_x, int index_start, int index_end)
{
	int i = 0;
	int index_result = index_start;
	for (i = index_start; i < index_end; i++)
	{
		if (ppData_x[i] > ppData_x[index_result])
			index_result = i;
	}
	return index_result;
}

int CSmath::Abs_Alg(int a)
{
	if (a < 0)
		return -a;
	else
		return a;
}

float CSmath::SmoothAverage_Alg(float last,float now)
{
	float ret = 0;
	ret = (0.9f*last+0.1f*now);
	printf("hr src %f, smooth %f\n", now, ret);
	return ret;
}

void CSmath::Sort_int(int * p_su, int numSort)
{
	int i = 0;
	int j = 0;
	long temp = 0;
	for (i = 0; i < numSort - 1; i++)
	{
		for (j = i + 1; j < numSort; j++)
		{
			if (p_su[i] < p_su[j])
			{
				temp = p_su[i];
				p_su[i] = p_su[j];
				p_su[j] = temp;
			}
		}
	}
}

int CSmath::abs_max(int * p, int left, int right)
{
	int i = 0;
	int temp_abs_max = abs(p[left]);
	int i_max = 0;
	for (i = left; i <= right; i++)
	{
		if (abs(p[i]) > temp_abs_max)
		{
			temp_abs_max = abs(p[i]);
			//i_max=i;
		}
	}
	return temp_abs_max;
}

int CSmath::chafen_2rd_Q(int * p_Data_x, int data_begin, int * p_2rd, int * p_1st, int R_point, int thr_Q, int Y1_B, int TimeareaQR)
{
	int i = 0;
	int tap = 1;
	int inflet_point = 0;

	//

	int Q_angle = 0;
	int num_tiaojian = 0;
	//
	for (i = R_point - 3; i > R_point - TimeareaQR; i--)
	{
		if ((p_2rd[i] - p_2rd[i - tap])*(p_2rd[i + 1] - p_2rd[i + 1 - tap]) <= 0)
		{
			if ((p_2rd[i])*(p_2rd[R_point]) <= 0)
			{
				//inflet_point=i;

				inflet_point = i - tap / 2;
				//break;
				///二阶差分判断
				if ((abs(p_2rd[inflet_point]) > thr_Q))
				{
					if (p_Data_x[data_begin + inflet_point] * p_Data_x[data_begin + R_point] > 0)//高度判断
					{
						if ((p_Data_x[data_begin + inflet_point] < MV1 / 10)
							|| (abs(p_Data_x[data_begin + inflet_point]) * 10 > abs(p_Data_x[data_begin + R_point]))
							)
						{
							num_tiaojian++;
						}
					}
					else
					{
						num_tiaojian++;
					}
					//角度判断
					//一阶差分判断
				}


				if (num_tiaojian > 0)
				{
					break;
				}
			}
		}
	}

	return inflet_point;
}

int CSmath::chafen_2rd_S(int * p_Data_x, int data_begin, int * p_2rd, int * p_1st, int R_point, int thr_S, int TimeareaRS)
{
	//p_Data_x 大数组数据，data_begin:R波在p_data中的位置；R_point，indexR 在p_2rd中的位置。
	int i = 0;
	int tap = 1;
	int inflet_point = 0;

	int S_angle = 0;
	int num_tiaojian = 0;
	//
	for (i = R_point + tap; i < R_point + SampleAlg / 10 - tap; i++)//此处3根据二阶差分丢掉的开始几个点
	{
		if ((p_2rd[i] - p_2rd[i + tap])*(p_2rd[i + 1] - p_2rd[i + 1 + tap]) <= 0)
		{
			if ((p_2rd[i])*(p_2rd[R_point]) <= 0)
			{
				//inflet_point=i;

				inflet_point = i + 1 + tap / 2;
				if (abs(p_2rd[inflet_point]) > thr_S)
				{
					num_tiaojian++;
				}
				if (num_tiaojian > 0)
				{
					break;
				}
			}
		}
	}
	return inflet_point;
}

int CSmath::Calculate_exact_S(int * p_ptr, int temp_S1, int time_S)
{
	int i = 0;
	int tap_S = 1;
	int S_station = 0;
	for (i = time_S; i < temp_S1 + 3; i++)
	{
		if ((p_ptr[i] - p_ptr[i - tap_S])*(p_ptr[i] - p_ptr[i + tap_S]) >= 0)
		{
			S_station = i;
			break;
		}
	}
	if (S_station < temp_S1 + 2)
	{
		S_station = temp_S1;
	}/**/
	return S_station;
}

int CSmath::calculate_chafen_banjing_for_Q(int * p_ptr, int left, int weizhi_R)
{
	int i = 0;
	int index_result = left;

	for (i = left; i <= weizhi_R; i++)
	{
		if (abs(p_ptr[i] - p_ptr[weizhi_R]) > abs(p_ptr[index_result] - p_ptr[weizhi_R]))
			index_result = i;
	}
	return weizhi_R - index_result;
}

int CSmath::calculate_chafen_banjing_for_S(int * p_ptr, int weizhi_R, int right)
{
	int i = 0;
	int index_result = right;

	for (i = weizhi_R; i <= right; i++)
	{
		if (abs(p_ptr[i] - p_ptr[weizhi_R]) > abs(p_ptr[index_result] - p_ptr[weizhi_R]))
			index_result = i;
	}
	return index_result - weizhi_R;
}


